﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DTO;

namespace HRProject.Asset
{
    public partial class RemoveAsset : System.Web.UI.Page
    {
        private Guid assetTypeId = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                try
                {
                    assetTypeId = new Guid(id);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                    assetTypeId = Guid.Empty;
                }
                AssetTypeDAO dao = new AssetTypeDAO();
                BLAssetType filter = new BLAssetType();
                filter.Id = assetTypeId;
                filter.Name = string.Empty;
                filter.Parent = new BLAssetType();
                filter.Parent.Id = Guid.Empty;
                List<BLAssetType> departments = dao.search(filter);
                if (departments.Count > 0)
                {
                    hdfId.Value = assetTypeId.ToString();
                    tbxName.Text = departments[0].Name;
                    tbxParent.Text = departments[0].Parent.Name;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsRemoveAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            BLAssetType assetType = new BLAssetType();
            try
            {
                assetTypeId = new Guid(hdfId.Value);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                assetTypeId = Guid.Empty;
            }
            assetType.Id = assetTypeId;
            assetType.Name = tbxName.Text;
            assetType.DataSignature = 0;

            AssetTypeDAO dao = new AssetTypeDAO();
            dao.delete(assetType);

            Util.CloseFrame(Response);
        }
    }
}
